﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using Symbonix.NorSoNet.Security.BLL.Membership;
using System.Net.Mail;
using Symbonix.NorSoNet.Security.BLL.Common;
using System.Web;

namespace Symbonix.NorSoNet.Security.BLL
{
    public class Workflow
    {
        public Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus RegisterMember(string username,
            string password, string email, string firstName, string lastName)
        {
            MembershipCreateStatus status = MembershipCreateStatus.ProviderError;
            try
            {
                // validating the register request with IP
                //if (!RequestValidator.ValidateRegisterRequest(HttpContext.Current.Request.UserHostAddress))
                //    return Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus.TooManyRequest;

                // creating user account
                IUser user = ModuleManager.MembershipFactory.GetUser().Create(username, password, email, firstName, lastName,
                    Symbonix.NorSoNet.Security.BLL.Options.UserRoles.Member.ToString(), false, out status);

                if (user == null)
                    return Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus.ServerError;

                // creating profile
                IUserProfile profile = ModuleManager.MembershipFactory.GetUserProfile().Create(user);

                if (profile == null)
                    return Options.RegistrationCompleteStatus.ProfileCreateFailed;

                // sending registration mail
                SecurityMailHelper mailHelper = SecurityMailHelper.GetInstance();

                
                mailHelper.SendRegistrationMail(user);

                return Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus.Success;

            }
            catch (SmtpException ex)
            {
                return Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus.MailSendFailed;
            }
            catch (Exception ex)
            {
                if (status == MembershipCreateStatus.DuplicateEmail)
                    return Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus.DuplicateEmail;
                else if (status == MembershipCreateStatus.DuplicateUserName)
                    return Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus.DuplicateUsername;
                else if (status == MembershipCreateStatus.InvalidPassword)
                    return Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus.InvalidPassword;
                else if (status == MembershipCreateStatus.InvalidEmail)
                    return Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus.InvalidEmail;
                else if (status == MembershipCreateStatus.InvalidUserName)
                    return Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus.InvalidUsername;
                else if (status == MembershipCreateStatus.ProviderError)
                    return Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus.ServerError;
                else
                    return Symbonix.NorSoNet.Security.BLL.Options.RegistrationCompleteStatus.ServerError;
            }
        }

        public void SendPasswordRecoveryLink(string username)
        {

            SecurityMailHelper mailHelper = NorSoNet.Security.BLL.SecurityMailHelper.GetInstance();
            mailHelper.SendPasswordRecoveryMail(username);

        }

        public void SetNewPassword(Guid activationKey, string email, string newPassword)
        {
            Symbonix.NorSoNet.Security.BLL.ModuleManager.MembershipFactory.GetUser().CreateNewPassword(activationKey, email, newPassword);
        }

        public bool ChangePassword(Guid activationKey, string email, string password)
        {
            IUser user = ModuleManager.MembershipFactory.GetUser();
            return user.CreateNewPassword(activationKey, email, password);

        }

        public string DeterimeLoginDestinationPage(string username)
        {
            IUser user = Symbonix.NorSoNet.Security.BLL.ModuleManager.MembershipFactory.GetUser().GetUserByUserName(username);
            if (user is Admin)
                return Symbonix.NorSoNet.Framework.CustomSiteMap.GetUrl(Symbonix.NorSoNet.Framework.CustomSiteMap.Name.AdminHome.ToString());
            else if(user is Member)
                return Symbonix.NorSoNet.Framework.CustomSiteMap.GetUrl(Symbonix.NorSoNet.Framework.CustomSiteMap.Name.MyProfile.ToString());
            else
                return Symbonix.NorSoNet.Framework.CustomSiteMap.GetUrl(Symbonix.NorSoNet.Framework.CustomSiteMap.Name.Login.ToString());
        }
    }
}
